// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue';
import App from './App';
import router from './router';
import axios from 'axios';
import MintUI from 'mint-ui';  //使用ui框架
import 'mint-ui/lib/style.css';  //引入ui框架的css
import iView from 'iview';
import 'iview/dist/styles/iview.css';    // 使用 CSS
import ElementUI from 'element-ui';
import './assets/css/element-#d50b0a/index.css' ;    // 引用element-ui框架主题
import Public from './assets/js/public';
import md5 from 'js-md5';  //引用md5加密插件
import { Loadmore } from 'mint-ui';  //引入上拉加载和下拉刷新
import Vuex from 'vuex'; //引用vuex
import store from './vuex/store';

import RongcloudCommon from './assets/js/RongcloudCommon'
import RongcloudConversation from './assets/js/RongcloudConversation'  //引用融云
import RongcloudMessage from './assets/js/RongcloudMessage'  //引用融云
import MuseUI from 'muse-ui'  //引用muse-ui框架
import 'muse-ui/dist/muse-ui.css'  //引用muse-ui框架的样式
import 'muse-ui/dist/theme-light.css' // 使用 carbon 主题




Vue.component(Loadmore.name, Loadmore); //引入上拉加载和下拉刷新
Vue.use(MintUI);  //声明使用mint-ui框架
Vue.use(iView);
Vue.use(ElementUI);
Vue.config.productionTip = false;		//不要调试提示
Vue.prototype.axios = axios;
Vue.prototype.Public = Public;
Vue.prototype.md5 = md5;
Vue.use(Vuex);  //使用vuex


Vue.use(MuseUI)  //声明使用muse-ui
Vue.prototype.RongcloudCommon = RongcloudCommon;  //挂载融云
Vue.prototype.RongcloudConversation = RongcloudConversation;  //挂载融云
Vue.prototype.RongcloudMessage = RongcloudMessage;  //挂载融云

Vue.directive('focus', {  //注册v-focus指令，用来让input自动获取focus
  inserted: function (el, binding) {
    el.focus()
  }
})

/* eslint-disable no-new */
var vm = new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: { App }
})

axios.interceptors.response.use(function (response) {
// 对响应数据做点什么
//  if(response.data.state==response.data.login){
//  	window.location.hash = '#/login'
//  }
    return response;
}, function (error) {
    // 对响应错误做点什么
    if("401"==error.response.status){
    	localStorage.removeItem("memberCode");
      vm.$router.push({path:"/landing"});//401即用户需要登录
    }
    return Promise.reject(error);
});

/* start 路由的before钩子 */
router.beforeEach((to,from,next) =>{
	if(to.matched.length === 0){
		next("/404"); //404错误页面
	}
	if (to.fullPath.indexOf(to.query["_k"]) != -1) {
		next();
	} else {
		//next();
		to.params["_k"] = routerRandomStr(6);
		let lastPos = to.fullPath.lastIndexOf("?_k=");
		if (lastPos == -1) {
			next(to.fullPath + "?_k=" + to.params["_k"]);
		} else {
			next(to.fullPath.substring(0, lastPos) + "?_k=" + to.params["_k"]);
		}
	}
});
function routerRandomStr(len) { //生成随机数，小写字母和数字
　　var $chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
　　var maxPos = $chars.length;
　　var pwd = '';
　　for (let i = 0; i < len; i++) {
　　　　pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
　　}
　　return pwd;
}
